(function(){
  'use strict';

  if (document.documentElement.getAttribute('data-router-bound')) return;
  document.documentElement.setAttribute('data-router-bound','1');

  var tabs = document.querySelectorAll('.tab-btn');
  var views = document.querySelectorAll('.view');
  var tabItems = document.querySelectorAll('.tab-item');

  function activate(target){
    for (var i=0;i<views.length;i++){
      var v=views[i]; var isTarget=v.id===target; v.classList.toggle('is-active', isTarget);
    }
    for (var j=0;j<tabItems.length;j++){
      var item=tabItems[j]; var btn=item.querySelector('.tab-btn'); var isActive=btn && btn.getAttribute('data-target')===target; item.classList.toggle('is-active', isActive); if(btn){ if(isActive){ btn.setAttribute('aria-current','page'); } else { btn.removeAttribute('aria-current'); } }
    }
  }

  function initFromHash(){
    var hash=location.hash.replace('#','')||'home'; var exists=document.getElementById(hash); activate(exists?hash:'home');
  }

  document.addEventListener('click', function(e){
    var t=e.target; var btn=t && (t.closest?t.closest('.tab-btn'):null); if(!btn) return; var target=btn.getAttribute('data-target'); if(!target) return; activate(target); history.replaceState(null,'','#'+target);
    
    // 触发特定页面的数据加载
    if (target === 'subscriptions') {
      console.log('路由: 切换到订阅页面，触发数据加载');
      // 延迟一点时间确保页面切换完成
      setTimeout(function() {
        if (window.subscriptionsManager && window.subscriptionsManager.loadList) {
          console.log('路由: 调用订阅管理器的loadList方法');
          window.subscriptionsManager.loadList();
        } else if (window.manualLoadSubscriptions) {
          console.log('路由: 调用手动加载订阅函数');
          window.manualLoadSubscriptions();
        } else {
          console.log('路由: 订阅管理器未找到，尝试直接触发hashchange事件');
          window.dispatchEvent(new HashChangeEvent('hashchange'));
        }
      }, 200);
    } else if (target === 'orders') {
      console.log('路由: 切换到订单页面，触发数据加载');
      // 延迟一点时间确保页面切换完成
      setTimeout(function() {
        if (window.ordersManager && window.ordersManager.loadOrders) {
          console.log('路由: 调用订单管理器的loadOrders方法');
          window.ordersManager.loadOrders();
        } else if (window.manualLoadOrders) {
          console.log('路由: 调用手动加载订单函数');
          window.manualLoadOrders();
        } else {
          console.log('路由: 订单管理器未找到，尝试直接触发hashchange事件');
          window.dispatchEvent(new HashChangeEvent('hashchange'));
        }
      }, 200);
    } else if (target === 'broadcast') {
      console.log('路由: 切换到广播页面，触发数据加载');
      // 延迟一点时间确保页面切换完成
      setTimeout(function() {
        console.log('路由: 触发广播页面数据加载');
        // 使用强制初始化函数
        if (window.broadcastManager && window.broadcastManager.forceInitBroadcast) {
          console.log('路由: 调用广播管理器的强制初始化方法');
          window.broadcastManager.forceInitBroadcast();
        } else {
          // 备选方案：直接调用加载函数
          if (window.broadcastManager && window.broadcastManager.loadGroups) {
            console.log('路由: 调用广播管理器的loadGroups方法');
            window.broadcastManager.loadGroups();
          }
          if (window.broadcastManager && window.broadcastManager.loadJoinedGroups) {
            console.log('路由: 调用广播管理器的loadJoinedGroups方法');
            window.broadcastManager.loadJoinedGroups();
          }
        }
        // 触发hashchange事件作为备选
        window.dispatchEvent(new HashChangeEvent('hashchange'));
      }, 200);
    }
  }, true);

  // CTA buttons with data-nav
  var ctas=document.querySelectorAll('[data-nav]');
  for(var i=0;i<ctas.length;i++){
    ctas[i].addEventListener('click', function(ev){ var target=ev.currentTarget.getAttribute('data-nav'); activate(target); history.replaceState(null,'','#'+target); });
  }

  initFromHash();
})();


